﻿using System.Text;
using System.Xml;
using System.Collections.Generic;

namespace BuzzX8.Xmpp.StreamElements
{
    public abstract class StreamElement
    {
        private Dictionary<string, object> attributes;
        private XmlWriterSettings writerSettings;
        private XmlNamespace elementNamespace;
        private string name;

        public StreamElement(string name) : this(name, "")
        {
        }

        public StreamElement(string name, string namespaceUri) : this(name, "", namespaceUri)
        {
        }

        public StreamElement(string name, string namespaceName, string namespaceUri)
        {
            this.name = name;
            elementNamespace = new XmlNamespace(namespaceName, namespaceUri);
            writerSettings = new XmlWriterSettings();
            writerSettings.OmitXmlDeclaration = true;
        }

        public string Name
        {
            get { return name; }
            protected set { name = value; }
        }

        public XmlNamespace Namespace
        {
            get { return elementNamespace; }
        }

        public StreamElement Parent { get; set; }

        protected IDictionary<string,object> Attributes
        {
            get { return attributes ?? (attributes = new Dictionary<string, object>()); }
        }

        protected XmlWriterSettings WriterSettings
        {
            get { return writerSettings; }
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            Write(sb);
            return sb.ToString();
        }

        protected XmlWriter CreateWriter(StringBuilder builder)
        {
            return XmlWriter.Create(builder, WriterSettings);
        }

        protected virtual void OnBeforeWrite()
        { }

        public void Write(StringBuilder stringBuilder)
        {
            XmlWriter writer = CreateWriter(stringBuilder);

            Write(writer);
            writer.Close();
        }

        public virtual void Write(XmlWriter writer)
        {
            OnBeforeWrite();
            WriteStartElement(writer);
            WriteAttributes(writer);
            WritePayload(writer);
            WriteEndElement(writer);
        }

        protected virtual void WriteStartElement(XmlWriter writer)
        {
            writer.WriteStartElement(Namespace.Name, Name, Namespace.Uri);
        }
                  
        protected virtual void WriteAttributes(XmlWriter writer)
        {
            if (attributes == null || attributes.Count == 0) return;
            foreach (KeyValuePair<string, object> attribute in attributes)
            {
                if (attribute.Value == null) continue;
                string value = attribute.Value.ToString();
                if (value == "") continue;
                writer.WriteAttributeString(attribute.Key, value);
            }
        }
                  
        protected virtual void WriteEndElement(XmlWriter writer)
        {
            writer.WriteEndElement();
        }

        protected virtual void WritePayload(XmlWriter writer)
        {
        }
    }
}
